﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Data;
using System.Collections;
using NLBS.Entity;
using System.Web.UI;
using System.Web;
using NLBS.Web;

namespace NLBS.Control
{
    public class PreviousAndNextLink : System.Web.UI.Control
    {

        private string CreateLink(string link, string css, string text, string linktwo)
        {
            return string.Format("<div class=\"{1}\"><a href=\"{3}\" onclick=\"{0}\">{2}</a></div>", link, css, text, linktwo);
            
        }
        protected override void Render(HtmlTextWriter writer)
        {
            IList<ArticleLink> lists = Cacher.GetPreviousAndNextArticleLink(Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]));
            UrlFormatsProvider urlFormats= UrlFormatsProvider.Instance();
            ReWriteUrls rewrite = ReWriteUrls.Instance();
            switch (lists.Count)
            {
                case 1:
                    writer.Write(CreateLink(urlFormats.ArticleCategoryUrl(lists[0].Id),
                        "category-label", "分类:" + lists[0].Title, rewrite.ArticleCategoryUrl(lists[0].Id)));
                    break;
                case 2:
                    if (lists[0].PostTime > lists[1].PostTime)
                    {
                        writer.Write(CreateLink(urlFormats.ArticleConent(lists[1].Id),
                            "prev-article", lists[1].Title, rewrite.ArticleCategoryUrl(lists[1].Id)));
                    }
                    else
                    {
                        writer.Write(CreateLink(urlFormats.ArticleConent(lists[1].Id),
                            "next-article", lists[1].Title, rewrite.ArticleCategoryUrl(lists[1].Id)));
                    }
                    writer.Write(CreateLink(urlFormats.ArticleCategoryUrl(lists[0].Id),
                        "category-label", "分类:" + lists[0].Title, rewrite.ArticleCategoryUrl(lists[0].Id)));
                    break;
                case 3:
                    writer.Write(CreateLink(urlFormats.ArticleConent(lists[1].Id),
                        "prev-article", lists[1].Title, rewrite.ArticleCategoryUrl(lists[1].Id)));
                    writer.Write(CreateLink(urlFormats.ArticleConent(lists[2].Id),
                        "next-article", lists[2].Title, rewrite.ArticleCategoryUrl(lists[2].Id)));
                    writer.Write(CreateLink(urlFormats.ArticleCategoryUrl(lists[0].Id), 
                        "category-label", "分类:" + lists[0].Title,
                        rewrite.ArticleCategoryUrl(lists[0].Id)
                        ));   
                    break;
            }
        }
    }
}
